(function ($) {
	$.createCourse = function () {
		return {
			init: function(){
				this.initLayui();
				this.listener();
			},
			/**
			 * 添加各按钮所需要的监听事件
			 */
			listener: function(){
				//点击资料中心添加
				$(".dataCenterFile").on("click",function(){
					//iframe窗

					parent.layer.open({
						type: 2,
						title: "请选择要导入资料",
						area: ['700px', '600px'],
						content: "pageFrame/dataCenterList.html"
						, btn: ['确定']
						, yes: function (index, layero) {
							var inputs = parent.layer.getChildFrame(".dataList li input:checked", index);
							$.each(inputs,function(i,data){
								var html='';
								html+='<div class="item">'
									+ ' 	<span class="fileName" data-url='+$(data).parents("li").data("fileurl")+'>'+$(data).parents("li").data("filename")+'</span>'
									+ ' 	<span class="fileDelete">[删除]</span>'
									+ ' </div>';
								$(".docList").append(html);
							});
							parent.layer.close(index);
						}
					});
				});
				//点击上传 触发事件
				$(".uploadcwp").on("click",function(){
					$("#uploadcwp").click();
				});
				$(".uploadFile").on("click",function(){
					$("#uploadFile").click();
				});
				$("#uploadFile").on("change", function () {
					$(".fileText").html("正在上传......");
					var list = document.getElementById("uploadFile").files;
					for (var index = 0; index < list.length; index++) {
						var listName = ($(this)[0].files)[index].name;
						if (list[index]) {
							uploadFile(SystemConfig.fileUploadUrl+ SystemConfig.appKey + "/", list[index], function (result) {
								var html = "";
								var result = JSON.parse(result);
								if (result.status == 1000) {
									//var docs = result.uploadFileDTO.fileId.split("/");
									//var docName=docs[docs.length-1];
									html+='<div class="item">'
										+ ' 	<span class="fileName" data-url='+result.uploadFileDTO.fileId+'>'+listName+'</span>'
										+ ' 	<span class="fileDelete">[删除]</span>'
										+ ' </div>';
									$(".docList").append(html);
									$(".fileText").html("");
								}
							});
						}
					}
					//置空file
					$(this).val("");
				});
				$("#uploadcwp").on("change", function () {
					$(".cwpText").html("正在上传......");
					if ($(this)[0].files.length) {
						var list = document.getElementById("uploadcwp").files;
						var listName = ($(this)[0].files)[0].name;
						var inputName = listName.split(".");
						var html = "";
						uploadFile(SystemConfig.fileUploadUrl+ SystemConfig.appKey + "/", list[0], function (result) {
							var result = JSON.parse(result);
							if (result.status == 1000) {
								//看上传的是ppt还是cwep
								if (inputName[inputName.length - 1] == "cwep") {
									html='<div class="item"><span class="cwpName" data-url=' + result.uploadFileDTO.fileId + '>'+listName+'</span><span class="cwpDelete">[删除]</span></div>';
									$(".cwpList").html(html);
								} else {
									//添加到附件ppt
									var pptHtml="";
									 pptHtml+='<div class="item">'
											+ ' 	<span class="fileName" data-url='+result.uploadFileDTO.fileId+'>'+listName+'</span>'
											+ ' 	<span class="fileDelete">[删除]</span>'
											+ ' </div>';
									$(".docList").append(pptHtml);
									$(".fileText").html("");
									//添加到cwep上传zip包
									var pptZip = {
										"url": result.uploadFileDTO.fileId
									};
									//更新
									updateObject("http://ppt2img.yunzuoye.net/XHPPTToImage/ppt/toimage", false, JSON.stringify(pptZip), function (result) {
										if (result.status == 1000) {
											html='<div class="item"><span class="cwpName" data-url=' + result.url + '>'+listName.split(".ppt")[0] +'.zip</span><span class="cwpDelete">[删除]</span></div>';
											$(".cwpList").html(html);
										}
									}, function () {
                                    });
								}
								$(".cwpText").html("");
							}
						});
					}
					//置空file
					$(this).val("");
				});
				$("#createCourse").on("click",".cwpDelete,.fileDelete",function(){
					$(this).parent().remove();
				});
			},
			initLayui: function () {
				layui.use(['layer', 'form'], function () {
					var $ = layui.jquery, layer = layui.layer, form = layui.form;
				});
			}
		}
	}();
    $.createCourse.init();
})(jQuery);